package org.iabako.client.ui.form.user.enterprise;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.InputElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.tools.IabakoAsyncCallback;
import org.iabako.client.ui.form.AbstractForm;
import org.iabako.client.ui.layout.field.IField;
import org.iabako.client.ui.layout.field.InputText;
import org.iabako.client.ui.layout.field.InputTextArea;
import org.iabako.client.ui.layout.field.LabelText;
import org.iabako.shared.entity.business.AbstractEntity;
import org.iabako.shared.entity.user.Enterprise;
import org.iabako.shared.entity.user.EnterpriseCustomInfo;
import org.iabako.shared.iservice.function.InvoiceGeneratorServiceAsync;
import org.iabako.shared.iservice.user.SignUpServiceAsync;
import org.iabako.shared.tools.GenericTools;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jose on 02/03/15.
 */
public class MyEnterpriseCustomInfoForm extends AbstractForm {

    interface MyUiBinder extends UiBinder<HTMLPanel, MyEnterpriseCustomInfoForm> {
    }

    protected MyUiBinder binderForm = GWT.create(MyUiBinder.class);

    protected final SignUpServiceAsync signUpServiceAsync = SignUpServiceAsync.Util.getInstance();
    protected final InvoiceGeneratorServiceAsync invoiceGeneratorServiceAsync = InvoiceGeneratorServiceAsync.Util.getInstance();


    @UiField
    LabelText newCustomInfo;
    @UiField
    HTMLPanel customInfoPanel;
    @UiField
    HTMLPanel invoiceSenderPanel;
    @UiField
    LabelText enterpriseName;
    @UiField
    LabelText enterpriseInfo;
    @UiField
    InputTextArea invoiceNote;
    @UiField
    LabelText downloadInvoice;
    @UiField
    FormPanel invoiceGeneratorForm;
    @UiField
    InputElement invoiceGeneratorFileToken;

    Map<InputText, InputText> customInfoFieldsMap;
    Map<InputText, EnterpriseCustomInfo> customInfoMap;
    MyEnterpriseConfigForm myEnterpriseConfigForm;

    public MyEnterpriseCustomInfoForm() {
        initWidget(binderForm.createAndBindUi(this));

        customInfoFieldsMap = new LinkedHashMap<InputText, InputText>();
        customInfoMap = new LinkedHashMap<InputText, EnterpriseCustomInfo>();

        setHandlers();
    }

    public void setMyEnterpriseConfigForm(MyEnterpriseConfigForm myEnterpriseConfigForm) {
        this.myEnterpriseConfigForm = myEnterpriseConfigForm;
    }

    @Override
    public void setVisible(boolean visible) {
        super.setVisible(visible);
        if (visible) {
            enterpriseName.setLabel(myEnterpriseConfigForm.enterpriseName.getValue());
            enterpriseInfo.setLabel(getEnterpriseInfo());
        }
    }

    private String getEnterpriseInfo() {
        String enterpriseInfo = "";
        if (!GenericTools.isEmpty(myEnterpriseConfigForm.addressLine1.getValue())) {
            enterpriseInfo += myEnterpriseConfigForm.addressLine1.getValue() + "\n";
        }
        if (!GenericTools.isEmpty(myEnterpriseConfigForm.addressLine2.getValue())) {
            enterpriseInfo += myEnterpriseConfigForm.addressLine2.getValue() + "\n";
        }
        if (!GenericTools.isEmpty(myEnterpriseConfigForm.zipCode.getValue())) {
            enterpriseInfo += myEnterpriseConfigForm.zipCode.getValue() + " - ";
        }
        if (!GenericTools.isEmpty(myEnterpriseConfigForm.city.getValue())) {
            enterpriseInfo += myEnterpriseConfigForm.city.getValue() + " - ";
        }
        if (!GenericTools.isEmpty(myEnterpriseConfigForm.country.getValue())) {
            enterpriseInfo += myEnterpriseConfigForm.country.getValue() + "\n";
        }
        if (!GenericTools.isEmpty(myEnterpriseConfigForm.phoneNumber.getValue())) {
            enterpriseInfo += myEnterpriseConfigForm.phoneNumber.getValue() + "\n";
        }
        if (!GenericTools.isEmpty(myEnterpriseConfigForm.email.getValue())) {
            enterpriseInfo += myEnterpriseConfigForm.email.getValue() + "\n";
        }
        if (!GenericTools.isEmpty(myEnterpriseConfigForm.webSite.getValue())) {
            enterpriseInfo += myEnterpriseConfigForm.webSite.getValue() + "\n";
        }
        return enterpriseInfo;
    }

    private void setHandlers() {
        newCustomInfo.getLabel().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                getCustomInputsLine(null, null, null);
            }
        });

        downloadInvoice.getLabel().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                Enterprise transientEnterprise = (Enterprise) getEntity();
                invoiceGeneratorServiceAsync.generateSampleInvoice(transientEnterprise, transientEnterprise.getCustomInfoList(), new IabakoAsyncCallback<String>() {
                    public void onSuccess(String result) {
                        invoiceGeneratorForm.setMethod("GET");
                        invoiceGeneratorFileToken.setValue(result);
                        invoiceGeneratorForm.submit();
                        super.onSuccess(result);
                    }
                });
            }
        });
    }

    private void getCustomInputsLine(EnterpriseCustomInfo customInfo, String titleString, String valueString) {
        final InputText title = new InputText("verysmallcolumnleft", "inputsize25 inputsmall", "Titulo");
        title.setValue(titleString);
        final InputText value = new InputText("", "inputsize30 inputsmall nextinput", "Valor");
        value.setValue(valueString);
        customInfoFieldsMap.put(title, value);
        customInfoMap.put(title, customInfo);

        final HTMLPanel deleteAction = new HTMLPanel("<div class='deleteicon invoicesenderdeletecustominfo'/>");
        deleteAction.setStyleName("floatleft");
        deleteAction.addDomHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                customInfoPanel.remove(title);
                customInfoPanel.remove(value);
                customInfoPanel.remove(deleteAction);
                customInfoFieldsMap.remove(title);

                if (customInfoFieldsMap.size() < 5) {
                    newCustomInfo.setVisible(true);
                }

            }
        }, ClickEvent.getType());

        customInfoPanel.add(title);
        customInfoPanel.add(value);
        customInfoPanel.add(deleteAction);

        if (customInfoFieldsMap.size() == 5) {
            newCustomInfo.setVisible(false);
        }
    }

    public void setEnterprise(Enterprise enterprise) {
        entity = enterprise;

        invoiceNote.setValue(enterprise.getInvoiceNote());

        signUpServiceAsync.getCustomInfoList(new IabakoAsyncCallback<List<EnterpriseCustomInfo>>() {
            public void onSuccess(List<EnterpriseCustomInfo> customInfoList) {
                for (EnterpriseCustomInfo customInfo : customInfoList) {
                    getCustomInputsLine(customInfo, customInfo.getTitle(), customInfo.getValue());
                }
                super.onSuccess(customInfoList);
            }
        });
    }

    @Override
    protected List<IField> getMandatoryFields() {
        return null;
    }

    @Override
    protected AbstractEntity getEntity() {
        return ((MyEnterpriseForm) parentForm).getEntity();
    }

    @Override
    public boolean validate() {
        return false;
    }
}
